﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace RepairTracker
{
    public partial class maintain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string lastChar;
            lastChar = User.Identity.Name.Substring(User.Identity.Name.Length - 1, 1);
            int permission=int.Parse(lastChar);

            if (permission<6)
            {

                Response.Write("权限不足");
                Response.End();
            }
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        private void RenderUI()
        {
            List<Device> devices = Entity.GetAllDevices();
            lstDevice.Items.Add(new ListItem("", "0".ToString()));
            foreach (Device device in devices)
            {
                lstDevice.Items.Add(new ListItem(device.DeviceName, device.Id.ToString()));

            }

            List<District> districts = Entity.GetAllDistricts();
            lstDistrict.Items.Add(new ListItem("", "0".ToString()));
            foreach (District district in districts)
            {
                lstDistrict.Items.Add(new ListItem(district.DistrictName, district.Id.ToString()));
            }

            lstOwner.Items.Add(new ListItem("", "0".ToString()));


            //set default viewid count on cookie
        }

        protected void lstDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            int districtId = int.Parse(lstDistrict.SelectedValue);
            if (districtId != 0)
            {
                List<Owner> owners = Entity.GetOwnersByDistrictId(districtId);
                lstOwner.Items.Clear();
                lstOwner.Items.Add(new ListItem("", "0".ToString()));
                foreach (Owner owner in owners)
                {
                    lstOwner.Items.Add(new ListItem(owner.DisplayName, owner.Id.ToString()));
                }

            }
        }

        protected void viewProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDevice.SelectedValue = viewProducts.SelectedDataKey["DeviceId"].ToString();
            lstDistrict.SelectedValue = viewProducts.SelectedDataKey["DistrictId"].ToString();
            lstDistrict_SelectedIndexChanged(null, null);
            lstOwner.SelectedValue = viewProducts.SelectedDataKey["OwnerId"].ToString();
            txtUpdateSerailNo.Text = viewProducts.SelectedDataKey["SerialNo"].ToString();
            txtUpdatePsamNo.Text = viewProducts.SelectedDataKey["PsamNo"].ToString();
            txtUpdateVersion.Text = viewProducts.SelectedDataKey["Version"].ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int deviceId, ownerId, id;
            string serialNo, psamNo, version;

            id = (int)viewProducts.SelectedDataKey["Id"];

            deviceId = int.Parse(lstDevice.SelectedValue);
            if (deviceId == 0)
            {
                lblUpdateError.Text = "请选择设备类型。";
                return;
            }

            ownerId = int.Parse(lstOwner.SelectedValue);
            if (ownerId == 0)
            {
                lblUpdateError.Text = "请选择网点。";
                return;
            }
            serialNo = txtUpdateSerailNo.Text.Replace("S/N:", "").Trim();
            if (serialNo.Length > 8)
            {
                lblUpdateError.Text = "序列号长度太长。";
                return;
            }
            else if (serialNo.Length < 8)
            {
                lblUpdateError.Text = "你的太短了！！！";
                return;
            }
            psamNo = txtUpdatePsamNo.Text.Replace("PSAM:", "").Trim();
            version = txtUpdateVersion.Text.Trim();

            bool ret =
                Entity.UpdateProduct(id, deviceId, ownerId, serialNo, psamNo, version);

            if (ret)
            {
                ResetUpdateControl();
                lblUpdateError.Text = "更新成功。";
            }
            else
            {

                lblUpdateError.Text = "更新失败。";
                
            }

        }

        private void ResetUpdateControl() 
        {
            lstDevice.SelectedIndex = -1;
            lstDistrict.SelectedIndex = -1;
            lstOwner.SelectedIndex = -1;
            lblUpdateError.Text = "";
            txtUpdateSerailNo.Text = "";
            txtUpdatePsamNo.Text = "";
            txtUpdateVersion.Text = "";
            viewProducts.SelectedIndex = -1;
            viewProducts.DataBind();
        }
    }
}